package com.ruoyi.web.controller.hsz;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.HszArticle;
import com.ruoyi.system.service.IHszArticleService;
import com.ruoyi.system.service.IHszClazzService;
import com.ruoyi.web.controller.hsz.param.HszArticleEditParam;
import com.ruoyi.web.controller.hsz.param.HszArticlePageParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static com.ruoyi.system.domain.table.HszArticleTableDef.HSZ_ARTICLE;

/**
 * 文章管理
 *
 * @author zhangqingshan
 * @date 2025-08-28
 */
@RestController
@RequestMapping("/hsz/article")
@Slf4j
public class HszArticleController {

    @Autowired
    private IHszArticleService hszArticleService;

    @Autowired
    private IHszClazzService hszClazzService;

    /**
     * 编辑
     * 权限：hsz:article:edit
     *
     * @param param 参数
     * @return {@link R }
     */
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('hsz:article:edit')")
    @Transactional(rollbackFor = Exception.class)
    public R edit(@RequestBody @Validated HszArticleEditParam param) {
        HszArticle article = param.toDO();
        // 校验分类
        if(Objects.isNull(hszClazzService.getById(article.getClazzId()))){
            return R.fail("分类不存在");
        }
        if (Objects.isNull(article.getArticleId())) { // 新增
            article.setCreater(SecurityUtils.getUserIdString());
        } else { // 修改
            article.setUpdater(SecurityUtils.getUserIdString());
        }
        return hszArticleService.saveOrUpdate(article)?R.ok():R.fail();
    }

    /**
     * 删除
     * 权限：hsz:article:del
     *
     * @param articleId 文章id
     * @return {@link R }
     */
    @DeleteMapping("/delete/{articleId}")
    @PreAuthorize("@ss.hasPermi('hsz:article:del')")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@PathVariable("articleId") Long articleId) {
        return hszArticleService.removeById(articleId)?R.ok():R.fail();
    }

    /**
     * 分页
     *
     * @param param 参数
     * @return {@link R }
     */
    @Anonymous
    @PostMapping("/page")
    public R<Page<HszArticle>> page(@RequestBody @Validated HszArticlePageParam param) {
        return R.ok(hszArticleService.page(param.toPage(), QueryWrapper.create().from(HszArticle.class)
                .where(HSZ_ARTICLE.CLAZZ_ID.in(hszClazzService.getChildClazzIds(param.getClazzId())).when(Objects.nonNull(param.getClazzId())))
                .and(HSZ_ARTICLE.TITLE.like(param.getTitle()).when(StringUtils.isNotBlank(param.getTitle())))
                .and(HSZ_ARTICLE.TYPE.eq(param.getType()).when(StringUtils.isNotBlank(param.getType())))
                .and(HSZ_ARTICLE.PUBLISH_ER.eq(param.getPublishEr()).when(StringUtils.isNotBlank(param.getPublishEr())))
                .and(HSZ_ARTICLE.IS_TOP.eq(param.getIsTop()).when(Objects.nonNull(param.getIsTop())))
                .and(HSZ_ARTICLE.IS_ROTATE.eq(param.getIsRotate()).when(Objects.nonNull(param.getIsRotate())))
                .and(HSZ_ARTICLE.PUBLISH_DATE.eq(param.getPublishDate()).when(Objects.nonNull(param.getPublishDate())))
                .and(HSZ_ARTICLE.PUBLISH_DATE.gt(param.getPublishDate()).when(Objects.nonNull(param.getPublishDateStart())))
                .and(HSZ_ARTICLE.PUBLISH_DATE.ne(param.getPublishDate()).when(Objects.nonNull(param.getPublishDateEnd())))
                .orderBy(HSZ_ARTICLE.IS_TOP.desc(), HSZ_ARTICLE.PUBLISH_DATE.desc(), HSZ_ARTICLE.CREATE_TIME.desc())));
    }

    /**
     * 详情
     *
     * @param articleId
     * @return {@link R }
     */
    @Anonymous
    @GetMapping("/detail/{articleId}")
    public R<HszArticle> detail(@PathVariable("articleId") Long articleId) {
        return R.ok(hszArticleService.getById(articleId));
    }
}
